package com.learncloud.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.learncloud.common.core.Constant;
import com.learncloud.common.core.exception.ServiceException;
import com.learncloud.common.core.wrapper.ResponseWrap;
import com.learncloud.common.core.wrapper.ResponseWrapUtil;
import com.learncloud.entity.VideoLabel;
import com.learncloud.entity.vo.VideoLabelVo;
import com.learncloud.service.IVideoLabelService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 标签表 前端控制器
 * </p>
 *
 * @author yj
 */
@RestController
@RequestMapping("/video/label")
public class VideoLabelController {

    @Autowired
    private IVideoLabelService iVideoLabelService;

    @PostMapping("/add")
    @ApiOperation(value = "新增视频标签")
    public ResponseWrap<VideoLabel> add(@RequestBody VideoLabel VideoLabel){
        boolean save = iVideoLabelService.save(VideoLabel);
        return ResponseWrapUtil.success();
    }

    @PostMapping ("/update")
    @ApiOperation(value = "修改视频标签")
    public ResponseWrap<VideoLabel> update(@RequestBody VideoLabel VideoLabel){
        if (Constant.Status.DISABLE.value.equals(VideoLabel.getStatus())){
            List<VideoLabel> list = iVideoLabelService.list(new QueryWrapper<>(new VideoLabel().setParentId(VideoLabel.getId()).setStatus(Constant.Status.ENABLE.value)));
            if (list.size() > 0){
                throw new ServiceException("禁用失败,请先禁用其子标签");
            }
        }
        boolean update = iVideoLabelService.updateById(VideoLabel);
        return ResponseWrapUtil.success();
    }

    @DeleteMapping("/del")
    @ApiOperation(value = "删除视频标签")
    public ResponseWrap<VideoLabel> del(Integer id){
        List<VideoLabel> list = iVideoLabelService.list(new QueryWrapper<>(new VideoLabel().setParentId(id).setStatus(Constant.Status.ENABLE.value)));
        if (list.size() > 0){
            throw new ServiceException("禁用失败,请先禁用其子类别");
        }
        boolean b = iVideoLabelService.removeById(id);
        return ResponseWrapUtil.success();
    }


    @GetMapping("/getList")
    @ApiOperation(value = "条件查询视频标签列表")
    public ResponseWrap<List<VideoLabelVo>> getList(VideoLabel videoLabel, Integer categoryId){
        List<VideoLabelVo> list = iVideoLabelService.getLabelVoList(videoLabel,categoryId);
        return ResponseWrapUtil.success(list);
    }

    @GetMapping("/getPaginationList")
    @ApiOperation(value = "条件分页查询视频标签列表")
    public ResponseWrap<Page> getPaginationList(Integer pageSize, Integer pageIndex, VideoLabel VideoLabel){
        Page page = iVideoLabelService.getPaginationList(new Page(pageIndex, pageSize),VideoLabel);
        return ResponseWrapUtil.success(page);
    }

}

